// by XIAO Yang
// from 2009-06-08


// Do a pause for xxx ms
function pause_ms(millis) {
	var date = new Date();
	var curDate = null;
	do {
		curDate = new Date();
	} while (curDate-date < millis);
}


//Get an element by id
function id(element_id) {
	return document.getElementById(element_id);
}

// Create a xlmHttp objet
function createXmlHttpObj () {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}

// GET html to fill a DIV
function ajaxGet(url, div){
	var xmlHttp = createXmlHttpObj();
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==1) {
			// display loading image here...
			var img_html = "<img src='images/loading.gif' alt='loading....' />";
			id(div).innerHTML=img_html;
		}
		if(xmlHttp.readyState==4) {
			id(div).innerHTML=xmlHttp.responseText;
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

//Counter for ajax object
var ajax_cnt = 0;

// GET html to fill a DIV
function ajaxGet(url, div){
	var xmlHttp = createXmlHttpObj();
	ajax_cnt++;
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState == 1) {
			// display loading image here...
			var pic = document.getElementById('img_ajax_loading');
			pic.src = "ajax-loader.gif";
		}
	    if(xmlHttp.readyState==4){
	      	document.getElementById(div).innerHTML=xmlHttp.responseText;
	      	ajax_cnt--;
	      	if(ajax_cnt==0) {
		      	//Hide image
				var pic = document.getElementById('img_ajax_loading');
				pic.src = "ok.png";
	      	}
	    }
    }
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

//check if a object exist by id
function chkObj(theVal) {
	if (document.getElementById(theVal) != null) {
		return true;
	} else {
		return false;
	}
}

//
function isSelected(obj){
	var slIdx= obj.selectedIndex;
	if (slIdx!=-1) {
		return true;
	}
	else
		return false;
}

//get value of a select
function getSelectValue(obj) {
	if(isSelected(obj)) {
		slIdx= obj.selectedIndex;
		return obj.options[slIdx].value;
	}
	else
		return false;
}

function getMultiSelectValue(selObj) {
	var selectedArray = new Array();
	for (var i=0; i<selObj.options.length; i++) {
		if (selObj.options[i].selected) {
			selectedArray.push(selObj.options[i].value);
		}
	}
	return selectedArray;
}

//unselect un select object
function unselectSelectOption(obj) {
	if(isSelected(obj)){
		obj.selectedIndex = -1;
	}
}

//get value of a radio group
function getRadioValue(obj) {
	for (var i=0; i<obj.length; i++) {
		if (obj[i].checked) {
			return obj[i].value;
		}
	}
}

function emptyDiv(div) {
	if(document.getElementById(div)!=null)
		document.getElementById(div).innerHTML = "";
}

// @format (hex|rgb|null) : Format to return, default is integer
function random_color(format) {
	var rint = Math.round(0xffffff * Math.random());
	switch(format) {
		case 'hex':
			return ('#0' + rint.toString(16)).replace(/^#0([0-9a-f]{6})$/i, '#$1');
			break;
  
		case 'rgb':
			return 'rgb(' + (rint >> 16) + ',' + (rint >> 8 & 255) + ',' + (rint & 255) + ')';
			break;
  
		default:
			return rint;
			break;
	}
}







// Dynamically loading external JavaScript and CSS files
function loadjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}
/* example
loadjscssfile("myscript.js", "js") //dynamically load and add this .js file
loadjscssfile("javascript.php", "js") //dynamically load "javascript.php" as a JavaScript file
loadjscssfile("mystyle.css", "css") ////dynamically load and add this .css file
*/